<?php
// +----------------------------------------------------------------------
// | 小牛Admin
// +----------------------------------------------------------------------
// | Website: www.xnadmin.cn
// +----------------------------------------------------------------------
// | Author: dav <85168163@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller\curd\service;

class BuildForm
{
    public function build($data)
    {
        $html = '';
        foreach ($data as $_data) {
            if ($_data['form_type'] == 'input') {
                $html .= $this->createInput($_data).PHP_EOL;
            } elseif ($_data['form_type'] == 'textarea') {
                $html .= $this->createTextarea($_data).PHP_EOL;
            } elseif ($_data['form_type'] == 'image') {
                $html .= $this->createImage($_data).PHP_EOL;
            } elseif ($_data['form_type'] == 'images') {
                $html .= $this->createImages($_data).PHP_EOL;
            } elseif ($_data['form_type'] == 'radio') {
                $html .= $this->createRadio($_data).PHP_EOL;
            } elseif ($_data['form_type'] == 'checkbox') {
                $html .= $this->createCheckbox($_data).PHP_EOL;
            } elseif ($_data['form_type'] == 'select') {
                $html .= $this->createSelect($_data).PHP_EOL;
            } elseif ($_data['form_type'] == 'switch') {
                $html .= $this->createSwitch($_data).PHP_EOL;
            } elseif ($_data['form_type'] == 'number') {
                $html .= $this->createNumber($_data).PHP_EOL;
            } elseif ($_data['form_type'] == 'editor') {
                $html .= $this->createEditor($_data).PHP_EOL;
            }
        }

        $html = str_replace('###', '$', $html);
        return $html;
    }


    /**
     * 文本
     * @param $arr
     * @return string
     */
    protected function createInput($data)
    {
        $required = $data['is_required'] ? 'required' : '';
        $html=<<<php
                <div class="layui-form-item">
                    <label class="layui-form-label">{$data['name']}</label>
                    <div class="layui-input-block">
                        <input type="text" name="{$data['field']}" lay-verify="{$required}" placeholder="请输入{$data['name']}" autocomplete="off" class="layui-input" value="{###data.{$data['field']}}">
                    </div>
                </div>
php;
        return $html;
    }

    /**
     * 数字
     * @param $data
     * @return string
     */
    protected function createNumber($data)
    {
        $required = $data['is_required'] ? 'required' : '';
        $html=<<<php
                <div class="layui-form-item">
                    <label class="layui-form-label">{$data['name']}</label>
                    <div class="layui-input-inline">
                        <input type="number" name="{$data['field']}" lay-verify="{$required}" placeholder="请输入{$data['name']}" autocomplete="off" class="layui-input" value="{###data.{$data['field']}}">
                    </div>
                </div>
php;
        return $html;
    }


    /**
     * 文本域
     * @param $data
     * @return string
     */
    protected function createTextarea($data)
    {
        $required = $data['is_required'] ? 'required' : '';
        $html=<<<php
                <div class="layui-form-item">
                    <label class="layui-form-label">{$data['name']}</label>
                    <div class="layui-input-block">
                        <textarea name="{$data['field']}" lay-verify="{$required}" placeholder="请输入{$data['name']}" class="layui-textarea">{###data.{$data['field']}}</textarea>
                    </div>
                </div>
php;
        return $html;
    }


    /**
     * 单图
     * @param $data
     * @return string
     */
    protected function createImage($data)
    {
        $html=<<<php
                <div class="layui-form-item">
                    <label class="layui-form-label">{$data['name']}</label>
                    <div class="layui-input-block">
                        {:xn_upload_one(###data.{$data['field']},'{$data['field']}')}
                    </div>
                </div>
php;
        return $html;
    }


    /**
     * 多图
     * @param $data
     * @return string
     */
    protected function createImages($data)
    {
        $html=<<<php
                <div class="layui-form-item">
                    <label class="layui-form-label">{$data['name']}</label>
                    <div class="layui-input-block">
                        {:xn_upload_multi(###data.{$data['field']},'{$data['field']}')}
                    </div>
                </div>
php;
        return $html;
    }


    /**
     * 单选
     * @param $data
     * @return string
     */
    protected function createRadio($data)
    {
        $rule = $data['form_rule'];
        if ( empty($rule) ) {
            retError('单选规则不能为空');
        }
        //$rule = explode(PHP_EOL, $rule);
        $rule = explode("\n", $rule);
        $radio_html = ''; // 单选html
        foreach ($rule as $k => $v) {
            $rule[$k] = trim($v);
            if ( empty($rule[$k]) ) {
                retError('单选规则不能为空');
            }
            $rule[$k] = explode('|', $rule[$k]);
            if ( count($rule[$k]) != 2 ) {
                retError('单选规则格式错误');
            }
            $radio_html .= <<<php
        <input type="radio" name="{$data['field']}" value="{$rule[$k][0]}" title="{$rule[$k][1]}" {if condition="###data['{$data['field']}'] eq '{$rule[$k][0]}'"}checked{/if}>
php;
            $radio_html .= PHP_EOL;
        }
        $radio_html = trim($radio_html);

        $html=<<<php
<div class="layui-form-item">
    <label class="layui-form-label">{$data['name']}</label>
    <div class="layui-input-block">
        {$radio_html}
    </div>
</div>
php;
        return $html;
    }

    /**
     * 多选 - 不支持：因为实际应用中，多选的值是数组，需要个性化处理
     * @param $data
     * @return string
     */
    protected function createCheckbox($data)
    {
        $html = '';
        return $html;
    }

    /**
     * 下拉
     * @param $data
     * @return string
     */
    protected function createSelect($data)
    {
        $required = $data['is_required'] ? 'required' : '';
        $rule = $data['form_rule'];
        if ( empty($rule) ) {
            retError('选规则不能为空');
        }
        //$rule = explode(PHP_EOL, $rule);
        $rule = explode("\n", $rule);
        $option_html = '';
        foreach ($rule as $k => $v) {
            $rule[$k] = trim($v);
            if ( empty($rule[$k]) ) {
                retError('选规则不能为空');
            }
            $rule[$k] = explode('|', $rule[$k]);
            if ( count($rule[$k]) != 2 ) {
                retError('选规则格式错误');
            }
            $option_html .= <<<php
<option value="{$rule[$k][0]}" {if condition="###data['{$data['field']}'] eq '{$rule[$k][0]}'"}selected{/if} >{$rule[$k][1]}</option>
php;
            $option_html .= PHP_EOL;
        }
        $option_html = trim($option_html);

        $html=<<<php
                <div class="layui-form-item">
                    <label class="layui-form-label">{$data['name']}</label>
                    <div class="layui-input-block">
                        <select name="{$data['field']}" lay-verify="{$required}">
                            <option value="">--请选择--</option>
                            {$option_html}
                        </select>
                    </div>
                </div>
php;
        return $html;
    }


    /**
     * 开关 - 暂不支持：因为当状态为关时没有值传入，导致修改时候没有修改这个值，建议用单选radio代替
     * @param $data
     * @return string
     */
    protected function createSwitch($data)
    {
        $html = '';
        return $html;
    }


    /**
     * 富文本
     * @param $data
     * @return string
     */
    protected function createEditor($data)
    {
        $html=<<<php
                <div class="layui-form-item">
                    <label class="layui-form-label">{$data['name']}</label>
                    <div class="layui-input-block">
                        <textarea name="{$data['field']}" id="{$data['field']}" style="width: 95%;">{###data.{$data['field']}}</textarea>
                    </div>
                </div>
php;
        return $html;
    }

    /**
     * 富文本JS
     * @param array $data 所有数据
     * @return string
     */
    public function buildEditorJs($data)
    {
        $_html = '';
        foreach ($data as $_data) {
            if ( $_data['form_type'] == 'editor' ) {
                $_html .= <<<php
    var ue_{$_data['field']} = UE.getEditor('{$_data['field']}', {
        //简洁编辑框
        //toolbars:[['FullScreen', 'Undo','Redo','underline','strikethrough','paragraph','fontsize','Bold','blockquote', '|', 'forecolor', 'backcolor', 'insertorderedlist','simpleupload', 'insertimage','inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown','splittocells', 'splittorows', 'splittocols', 'link', 'unlink','insertcode']],
        initialFrameHeight:300,// 设置编辑器高度
        scaleEnabled:true,
        serverUrl:"{:url('UeditorUpload/index')}"
    })

php;
            }
        }
        $html=<<<php
<!--编辑器-->
<script src="/static/admin/ueditor/ueditor.config.js"></script>
<script src="/static/admin/ueditor/ueditor.all.min.js"></script>
<script src="/static/admin/ueditor/lang/zh-cn/zh-cn.js"></script>
<script>
{$_html}
</script>
php;
        return $html;
    }
}
